#include "modulesmanager.h"

ModulesManager::ModulesManager()  {
    m_pathFile =  QCoreApplication::applicationDirPath() + "/config.ini";

    m_configure = new QSettings(m_pathFile, QSettings::IniFormat);
    m_configure->setIniCodec("UTF8");
    QString str = QString("module/path");
    QStringList pathlist = m_configure->value(str).toString().split(":");
    for(int i = 0; i < pathlist.size(); ++i) {
        if(!m_vpath.contains(pathlist.at(i)) && pathlist.at(i) != "") {
            m_vpath.push_back(pathlist.at(i));
        }
    }

    delete m_configure;
}

ModulesManager::~ModulesManager() {
    for(int i = 0; i < m_vme.size(); ++i) {
        Destory fn = (Destory)m_vme.at(i)->lib->resolve("DestroyWidget");
        fn();
        delete m_vme.at(i)->topbtn;
        m_vme.at(i)->lib->unload();
        delete m_vme.at(i)->mobtn;
    }
}

VME ModulesManager::LoadModule() {
    for(int i = 0; i < m_vpath.size(); ++i) {
        QString path = m_vpath[i];
        QIcon* icon;
        QString* text;
        ModuleElem* me;

        me = new ModuleElem;

        me->lib = new QLibrary(path);

        if(me->lib->load()) {
            me->topbtn = new QPushButton;
            me->mobtn = new ModuleBtn;
            me->widget = new QWidget;
            CreateFunc createfn = (CreateFunc)me->lib->resolve("CreateWidget");
            if(createfn) {
                me->widget = createfn(me->widget);
                IModules* module = dynamic_cast<IModules*>(me->widget);
                if(module) {
                    icon = module->GetModuleIcon();
                    text = module->GetModuleText();

                    InterfaceManager* intermanager = new InterfaceManager("/tmp/oracledb.ini");
                    module->setSysCtrl(intermanager);

                    me->mobtn->SetModuleIcon(icon);
                    QString tmp = *text;
                    me->mobtn->SetModuleText(tmp);
                    me->topbtn->setIcon(*icon);
                    me->topbtn->setText("  " + tmp);
                    me->topbtn->resize(200, 40);
                    me->topbtn->setIconSize(QSize(25, 25));

                    delete icon;
                    delete text;

                    m_vme.push_back(me);
                }
                else {
                    continue;
                }
            }
        }
        else
        {
            cout << me->lib->errorString();
            continue;
        }
    }

    return m_vme;
}

VME ModulesManager::AddModules() {
    m_configure = new QSettings(m_pathFile, QSettings::IniFormat);
    m_configure->setIniCodec("UTF8");
    int size = m_vpath.size();
    QString str = QString("module/path");
    QStringList pathlist = m_configure->value(str).toString().split(":");
    for(int i = 0; i < pathlist.size(); ++i) {
        if(!m_vpath.contains(pathlist.at(i)) && pathlist.at(i) != "") {
            m_vpath.push_back(pathlist.at(i));
        }
    }

    delete m_configure;

    for(int i = size; i < m_vpath.size(); ++i) {
        QString path = m_vpath[i];
        QIcon* icon;
        QString* text;
        ModuleElem* me;

        me = new ModuleElem;

        me->lib = new QLibrary(path);
        if(me->lib->load()) {
            me->topbtn = new QPushButton;
            me->mobtn = new ModuleBtn;
            me->widget = new QWidget;
            CreateFunc createfn = (CreateFunc)me->lib->resolve("CreateWidget");
            if(createfn) {
                me->widget = createfn(me->widget);
                IModules* module = dynamic_cast<IModules*>(me->widget);
                if(module) {
                    icon = module->GetModuleIcon();
                    text = module->GetModuleText();
                    me->mobtn->SetModuleIcon(icon);
                    QString tmp = *text;
                    me->mobtn->SetModuleText(tmp);
                    me->topbtn->setIcon(*icon);
                    me->topbtn->setText("  " + tmp);
                    me->topbtn->resize(200, 40);
                    me->topbtn->setIconSize(QSize(25, 25));

                    delete icon;
                    delete text;
                }
                else {
                    continue;
                }
            }
            m_vme.push_back(me);
        }
        else
        {
            cout << me->lib->errorString();
            continue;
        }
    }

    return m_vme;
}

VME ModulesManager::DelModule(const int& index) {
    m_vpath.clear();

    m_configure = new QSettings(m_pathFile, QSettings::IniFormat);
    m_configure->setIniCodec("UTF8");
    QString str = QString("module/path");
    QStringList pathlist = m_configure->value(str).toString().split(":");
    for(int i = 0; i < pathlist.size(); ++i) {
        if(!m_vpath.contains(pathlist.at(i)) && pathlist.at(i) != "") {
            m_vpath.push_back(pathlist.at(i));
        }
    }
    delete m_configure;

    Destory fn = (Destory)m_vme.at(index)->lib->resolve("DestroyWidget");
    fn();
    delete m_vme.at(index)->topbtn;
    m_vme.at(index)->lib->unload();
    delete m_vme.at(index)->mobtn;
    m_vme.erase(m_vme.begin() + index);
    return m_vme;
}
